package dataStructure;


import java.util.Arrays;
import java.util.Set;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import android.util.Log;


public class HandleXML extends DefaultHandler {

	private DataObject currentObject = null;
	private DataObject parentObject = null;
	private Boolean currentElement = false;
	private String currentValue = null;
	private int levelCntr = 0;
	
	private static final String[] whiteList = new String[] {"WMS_Capabilities", "Capability", "Layer", "Name", "Title", "EX_GeographicBoundingBox", "westBoundLongitude", "eastBoundLongitude", "southBoundLatitude", "northBoundLatitude"};//list of tags used tags for optimalisation
	int notWhiteListedLvl = 0; //incremented every time when a tag not in the white list is opened, decremented every time a tag not in the whiteList is closed 
	
	
	@Override
	public void startElement(String uri, String localName, String qName,
	Attributes attributes) throws SAXException {
		
		currentElement = true;
		
		if(notWhiteListedLvl == 0 && Arrays.asList(whiteList).contains(localName.toString()))
		{
			if (localName.equals("WMS_Capabilities"))												//links object to DataCore as first object in file
			{							
				currentObject = new DataObject(localName, null);
				DataCore.contentObject = currentObject;
				parentObject = currentObject;
			}
			else if (localName.equals("Layer") && currentObject.getTagName().equals("Capability"))	//root level layer object
			{			
				levelCntr++;
				parentObject = currentObject;
				currentObject = new DataLayerObject(localName, parentObject);
				DataCore.layerObject = (DataLayerObject) currentObject; 							//add the object to DataCore.layerObject for easy retrieval of information
				parentObject.addChild(currentObject);
				
				currentObject.setType(ObjectType.LAYER);
				((DataLayerObject) currentObject).setLevel(levelCntr);								//set level to 1 (root)
			}
			else if (localName.equals("Layer"))
			{			
				levelCntr++;
				parentObject = currentObject;
				currentObject = new DataLayerObject(localName, parentObject);
				 parentObject.addChild(currentObject);
				
				 ((DataLayerObject)currentObject).setType(ObjectType.LAYER);
				 ((DataLayerObject) currentObject).setLevel(levelCntr);
			}
			else if (currentObject.getType() == ObjectType.LAYER)									//notice that currentObject is the parent of the 
			{
				parentObject = currentObject;
				currentObject = new DataLayerObject(localName, parentObject);
				((DataLayerObject) parentObject).addChild(currentObject);
			}
			else
			{
				parentObject = currentObject;
				currentObject = new DataObject(localName, parentObject);
				parentObject.addChild(currentObject);
			}
		}
		else
		{
			notWhiteListedLvl++;
		}
	
	}
	
	/** Called when tag closing ( ex:- <name>AndroidPeople</name>
	* -- </name> )*/
	@Override
	public void endElement(String uri, String localName, String qName)
	throws SAXException {
		
		currentElement = false;
		
		if(notWhiteListedLvl == 0 && Arrays.asList(whiteList).contains(localName.toString()))
		{
			if (!localName.equalsIgnoreCase("WMS_Capabilities"))
			{
				if (!parentObject.getTagName().equalsIgnoreCase("WMS_Capabilities") && parentObject.getType() == ObjectType.LAYER)//objects inside layers
				{
					if(localName.equalsIgnoreCase("Name"))
					{
						((DataLayerObject) parentObject).setName(currentValue);	
					}
					if(localName.equalsIgnoreCase("Title"))
					{
						((DataLayerObject) parentObject).setTitle(currentValue);
					}													
					if(currentObject.getType() != ObjectType.LAYER)
					{
						parentObject.getChildren().remove(currentObject);//remove currentObject from Layer cleanup
					}
					
					currentObject = parentObject;
				}
				else if (parentObject.getTagName().equalsIgnoreCase("EX_GeographicBoundingBox"))
				{			
							
					if(parentObject.getParentObject().getType() == ObjectType.LAYER) 
					{
						
						if(localName.equalsIgnoreCase("westBoundLongitude"))//localName.equalsIgnoreCase("westBoundLongitude"))
						{						
							((DataLayerObject) parentObject.getParentObject()).setWest(Double.parseDouble(currentValue));	
						}
						else if(localName.equalsIgnoreCase("eastBoundLongitude"))
						{
							((DataLayerObject) parentObject.getParentObject()).setEast(Double.parseDouble(currentValue));
						}
						else if(localName.equalsIgnoreCase("southBoundLatitude"))
						{
							((DataLayerObject) parentObject.getParentObject()).setSouth(Double.parseDouble(currentValue));
						}
						else if(localName.equalsIgnoreCase("northBoundLatitude"))
						{
							((DataLayerObject) parentObject.getParentObject()).setNorth(Double.parseDouble(currentValue));
						}
						
						
						DataObject tempParent = currentObject.getParentObject();
						
						if(currentObject.getType() != ObjectType.LAYER)
						{
							tempParent.getChildren().remove(currentObject);//remove currentObject from Layer cleanup
						}
									
						currentObject = tempParent;
					}
				}
				else//normal objects
				{
					currentObject.setValue(currentValue);
					currentObject.setType(ObjectType.VALUE);			
					currentObject = currentObject.getParentObject();
				}
			}
						
			if (localName.equalsIgnoreCase("WMS_Capabilities"))
			{
				
			}
			else
			{
				
				if (localName.equalsIgnoreCase("Layer"))//lower levelCntr while closing layer object
				{
					levelCntr--;
				}
				
				parentObject = currentObject.getParentObject();
			}
		}
		else
		{
			notWhiteListedLvl--;
		}
	}
	
	/** Called to get tag characters ( ex:- <name>AndroidPeople</name>
	* -- to get AndroidPeople Character ) */
	@Override
	public void characters(char[] ch, int start, int length)
	throws SAXException {
	
		if (currentElement) {
		currentValue = new String(ch, start, length);
		
		currentElement = false;
		}

	}
}
